/*
 * *****************************************************************************
 * Copyright (C) 2014-2023 Dennis Sheirer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * ****************************************************************************
 */

package io.github.dsheirer.dsp.oscillator;

import io.github.dsheirer.sample.complex.ComplexSamples;
import java.util.Random;

/**
 * Additive white gaussian noise wrapper around a complex oscillator that mixes in gaussian noise to the complex
 * samples generated by the underlying complex oscillator.
 */
public class AWGNOscillator implements IComplexOscillator
{
    private final Random mRandom = new Random();
    private IComplexOscillator mComplexOscillator;
    private float mNoiseScale;

    /**
     * Constructs an instance
     * @param complexOscillator to generate samples
     * @param noiseScale to apply to generated samples to reduce the power of the noise and increase the SNR.
     */
    public AWGNOscillator(IComplexOscillator complexOscillator, float noiseScale)
    {
        mComplexOscillator = complexOscillator;
        mNoiseScale = noiseScale;
    }

    /**
     * Produce the next scaled noise sample.
     * @return next scaled noise sample.
     */
    private float nextNoise()
    {
        return (float)mRandom.nextGaussian() * mNoiseScale;
    }

    @Override
    public float[] generate(int sampleCount)
    {
        float[] generated = mComplexOscillator.generate(sampleCount);

        for(int x = 0; x < generated.length; x++)
        {
            generated[x] += nextNoise();
        }

        return generated;
    }

    @Override
    public ComplexSamples generateComplexSamples(int sampleCount, long timestamp)
    {
        ComplexSamples samples = mComplexOscillator.generateComplexSamples(sampleCount, timestamp);

        for(int x = 0; x < samples.i().length; x++)
        {
            samples.i()[x] += nextNoise();
            samples.q()[x] += nextNoise();
        }

        return samples;
    }

    @Override
    public double getFrequency()
    {
        return mComplexOscillator.getFrequency();
    }

    @Override
    public void setFrequency(double frequency)
    {
        mComplexOscillator.setFrequency(frequency);
    }

    @Override
    public boolean hasFrequency()
    {
        return mComplexOscillator.hasFrequency();
    }

    @Override
    public double getSampleRate()
    {
        return mComplexOscillator.getSampleRate();
    }

    @Override
    public void setSampleRate(double sampleRate)
    {
        mComplexOscillator.setSampleRate(sampleRate);
    }

    @Override
    public boolean isEnabled()
    {
        return mComplexOscillator.isEnabled();
    }
}
